package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.dao.jpa.LmbCouPurPlanDAO;
import com.trkj.dao.jpa.LmbCousumablePurchaseDAO;
import com.trkj.dao.jpa.LmbPurchaseDetailDAO;
import com.trkj.dao.jpa.LmbSupplierDAO;
import com.trkj.dao.jpa.LmbTbstaffDAO;
import com.trkj.pojos.LmbCousumablePurchase;
import com.trkj.pojos.LmbCousumablePurchasePlan;
import com.trkj.pojos.LmbPurchaseDetail;
import com.trkj.pojos.Tbstaff;
import com.trkj.vo.LmbCouPurVo;
import com.trkj.vo.LmbCousumablePurchaseVo;
import com.trkj.vo.LmbCurPurListVo;
import com.trkj.vo.LmbPurchaseDetailVo;

@Service
@Transactional
public class LmbCousumablePurchaseService {
	@Autowired
	private LmbCousumablePurchaseDAO couPurDAO;	
	@Autowired
	private LmbTbstaffDAO staffDAO;
	@Autowired
	private LmbCouPurPlanDAO couPurPlanDAO;
	@Autowired
	private LmbPurchaseDetailDAO purchaseDetailDAO;
	@Autowired
	private LmbSupplierDAO supplierDAO;
	@Autowired
	private IYzrTbstaffDAO yzrTbstaffDAO;
	
	/**

	 * 查询所有的采购单

	 * @return

	 */
	public List<LmbCousumablePurchaseVo> findAllCouPur(){
		List<LmbCousumablePurchase> list = (List<LmbCousumablePurchase>) couPurDAO.findAll();	
		return poLmbCouPurList2VoList(list);	
	}
	
	/**

	 * 将耗材采购的po转换成Vo

	 * @param staff

	 * @return

	 */
	private LmbCousumablePurchaseVo cousumablePurchase2Vo(LmbCousumablePurchase coupur){
		LmbCousumablePurchaseVo cousumablePurchaseVo = new LmbCousumablePurchaseVo();
		cousumablePurchaseVo.setCpId(coupur.getCpId());
		cousumablePurchaseVo.setCpPlId(coupur.getCpPlId());
		cousumablePurchaseVo.setCpStatus(coupur.getCpStatus());
		cousumablePurchaseVo.setCpTime(coupur.getCpTime());
		System.out.println(coupur.getCpRsStatus());
		cousumablePurchaseVo.setCpRsStatus(coupur.getCpRsStatus());
		BigDecimal sid = coupur.getCpBuyer();	
		if(sid != null){
			long Sid = sid.longValue();
			Tbstaff staff = staffDAO.findOne(Sid);
			cousumablePurchaseVo.setSid(Sid);
			System.out.println("staff.getStaffName():"+staff.getStaffName());
			cousumablePurchaseVo.setSname(staff.getStaffName());
		}else{
			cousumablePurchaseVo.setSid(0);
			cousumablePurchaseVo.setSname(null);
		}		
		return cousumablePurchaseVo;		
	}
	
	/**

	 * 将LmbCousumablePurchase的集合转换成LmbCousumablePurchaseVo的集合

	 * @param list

	 * @return

	 */
	private List<LmbCousumablePurchaseVo> poLmbCouPurList2VoList(List<LmbCousumablePurchase> list){
		if(list == null) return null;
		List<LmbCousumablePurchaseVo> listVo = new ArrayList<>();
		for(LmbCousumablePurchase couPur: list){
			LmbCousumablePurchaseVo couPurVo = cousumablePurchase2Vo(couPur);
			listVo.add(couPurVo);
		}
		return listVo;	
	}
	
	public void addCousumablePurchase(LmbCurPurListVo curPurListVo){
		//保存采购计划的基本信息
		// 获取日期
		LmbOddNumbersService oddNumbersService = new LmbOddNumbersService();
		String date = oddNumbersService.rq();
		//拼接
		String name = "HC-" + date;					
		String n = couPurDAO.findMaxID(name);	
		System.out.println("从数据库中读取的最大采购计划编号："+n);
		String sid =oddNumbersService.getId(name, n);
		LmbCousumablePurchase cp =  curPurListVo.getCousumablePurchase();
		cp.setCpId(sid);
		cp.setCpStatus("未验收");
		cp.setCpYsStatus("未验收");
		cp.setCpRsStatus("未入库");
		couPurDAO.save(cp);
		LmbCousumablePurchasePlan cousumablePurchasePlan = couPurPlanDAO.findOne(cp.getCpPlId());
		cousumablePurchasePlan.setCpPlanPurstatus("已采购");
		couPurPlanDAO.save(cousumablePurchasePlan);
		List<LmbPurchaseDetail> list=curPurListVo.getPurchaseDetail();
		if(list != null){	
			//创建采购计划明细与采购计划的关系
			for(LmbPurchaseDetail detail:list){
				//拼接
				String name1 = "HCX-" + date;	
				String n1 = purchaseDetailDAO.findMaxID(name1);	
				String sid1 =oddNumbersService.getId(name1, n1);
				System.out.println("date"+detail.getPdProductDate());
				detail.setPdId(sid1);		
				detail.setPdCpId(sid);
				purchaseDetailDAO.save(detail);
			}					
		}	
	}
	
	public LmbCouPurVo findCouPurById(String id){
		return curPurPo2Vo(couPurDAO.findOne(id));		
	}
	
	public LmbCouPurVo curPurPo2Vo(LmbCousumablePurchase couPurchase){
		LmbCouPurVo couPurVo = new LmbCouPurVo();
		couPurVo.setCpId(couPurchase.getCpId());
		couPurVo.setCpPlId(couPurchase.getCpPlId());
		couPurVo.setCpRsStatus(couPurchase.getCpRsStatus());
		couPurVo.setCpTime(couPurchase.getCpTime());
		couPurVo.setCpYsStatus(couPurchase.getCpYsStatus());
		couPurVo.setCpBuyer(couPurchase.getCpBuyer());
		couPurVo.setCpStatus(couPurchase.getCpStatus());
		BigDecimal sid = couPurchase.getCpBuyer();	
		if(sid != null){
			System.out.println("不为空");
			long id = sid.longValue();
			System.out.println("id为："+id);
			Tbstaff staff = yzrTbstaffDAO.findOne(id);
			couPurVo.setName(staff.getStaffName());
		}else{
			System.out.println("为空");
			couPurVo.setName(null);
		}
		return couPurVo;
	}
	
	public List<LmbCouPurVo> poCouPurList2VoList(List<LmbCousumablePurchase> list){
		if(list == null) return null;
		List<LmbCouPurVo> listVo = new ArrayList<>();
		for(LmbCousumablePurchase cousumablePurchase: list){
			LmbCouPurVo couPurVo = curPurPo2Vo(cousumablePurchase);
			listVo.add(couPurVo);
		}
		return listVo;
	}
	
	public LmbPurchaseDetailVo curPurDetPo2Vo(LmbPurchaseDetail purchaseDetail){
		LmbPurchaseDetailVo purchaseDetailVo = new LmbPurchaseDetailVo();
		purchaseDetailVo.setPdId(purchaseDetail.getPdId());
		purchaseDetailVo.setPdCpId(purchaseDetail.getPdCpId());
		purchaseDetailVo.setPdCId(purchaseDetail.getPdCId());
		purchaseDetailVo.setPdName(purchaseDetail.getPdName());
		purchaseDetailVo.setPdBatchNum(purchaseDetail.getPdBatchNum());
		purchaseDetailVo.setPdNorms(purchaseDetail.getPdNorms());
		purchaseDetailVo.setPdNum(purchaseDetail.getPdNum());
		purchaseDetailVo.setPdPrice(purchaseDetail.getPdPrice());
		purchaseDetailVo.setPdProductDate(purchaseDetail.getPdProductDate());
		purchaseDetailVo.setPdSupplier(purchaseDetail.getPdSupplier());
		purchaseDetailVo.setPdValidTill(purchaseDetail.getPdValidTill());
		purchaseDetailVo.setSupSid(purchaseDetail.getPdSupplier());
		purchaseDetailVo.setSupName(supplierDAO.findID(purchaseDetail.getPdSupplier()));
		return purchaseDetailVo;
	}
	
	public List<LmbPurchaseDetailVo> poCouPurDetList2VoList(List<LmbPurchaseDetail> list){
		if(list == null) return null;
		List<LmbPurchaseDetailVo> listVo = new ArrayList<>();
		for(LmbPurchaseDetail purchaseDetail: list){
			LmbPurchaseDetailVo purchaseDetailVo = curPurDetPo2Vo(purchaseDetail);
			listVo.add(purchaseDetailVo);
		}
		return listVo;
	}
	
	public List<LmbPurchaseDetailVo> findPurchaseDetailVo(String id){
		return poCouPurDetList2VoList(purchaseDetailDAO.findPurchaseDetail(id));
	}
	
	public Map<String,Object> findLmbCouPurVoByIf(String name,String shuxing,int page,int rows){
		PageRequest pr = new PageRequest(page - 1,rows);
		Map<String,Object> map = new HashMap<>();
		if(name.equals("ddbh")){
			List<LmbCousumablePurchase> couPur = couPurDAO.findCousPurchaseByCpId(shuxing, pr);
			long total = couPurDAO.countCousPurchaseByCpId(shuxing);
			map.put("total", total);
			map.put("rows",poLmbCouPurList2VoList(couPur));
		}else if(name.equals("yszt")){
			List<LmbCousumablePurchase> couPur = couPurDAO.findCousPurchaseByCpStatus(shuxing, pr);
			long total = couPurDAO.countCousPurchaseByCpStatus(shuxing);
			map.put("total", total);
			map.put("rows",poLmbCouPurList2VoList(couPur));
		}else if(name.equals("rkzt")){
			List<LmbCousumablePurchase> couPur = couPurDAO.findCousPurchaseByCpRsStatus(shuxing, pr);
			long total = couPurDAO.countCousPurchaseByCpRsStatus(shuxing);
			map.put("total", total);
			map.put("rows",poLmbCouPurList2VoList(couPur));
		}
		return map;		
	}
	
	public Map<String,Object> findBytj(int page,int rows){
		PageRequest pr = new PageRequest(page - 1,rows);
		Map<String,Object> map = new HashMap<>();
		List<LmbCousumablePurchasePlan> list = couPurPlanDAO.findBytj(pr);
		long total = couPurPlanDAO.CountFindBytj();
		map.put("total", total);
		map.put("rows", list);
		return map;		
	}
}
